﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameManager : MonoBehaviour
{
	public string gameMode { get; protected set; }

	public IBoardDisplay mainBoardDisplay { get; protected set; }

	public Board mainBoard { get; protected set; }

	public Player mainPlayer { get; protected set; }

	public SpriteManager mainSpriteManager { get; protected set; }

	public XMLGems mainXMLGems { get; protected set; }

	public AutoServer mainServer { get; protected set; }

	public AutoClient mainClient { get; protected set; }

    public RemotePlayer mainRemotePlayer { get; protected set; }

	public EffectsManager mainEffectsManager { get; protected set; }

    public Transform mainPanel;
	// Testing-only variables

    public int boardDim = 8;
	void Start()
	{
        mainServer = Data.gameServer;
        mainClient = Data.gameClient;
        gameMode = Data.gameMode;

		GameObject newObject = new GameObject("XMLGems");
		mainXMLGems = newObject.AddComponent<XMLGems>();
		mainXMLGems.getAllAtribsInGem();

		newObject = new GameObject("EffectsManager");
		mainEffectsManager = newObject.AddComponent<EffectsManager>();
		//mainEffectsManager.Init(this);

		newObject = new GameObject("SpriteManager");
		mainSpriteManager = newObject.AddComponent<SpriteManager>();
		mainSpriteManager.atlasPath = "Atlases/";
		mainSpriteManager.LoadAtlas("default");

		newObject = new GameObject("Board");
        if (Data.multiplayer && mainServer == null)
        {
            Debug.Log("Creating remote board...");
            mainBoard = newObject.AddComponent<BoardRemote>();
        }
        else
        {
            Debug.Log("Creating local board...");
            mainBoard = newObject.AddComponent<BoardLocal>();
        }

		newObject = new GameObject("BoardDisplay");
        mainPanel = newObject.transform;
        mainEffectsManager.Init(this);
		mainBoardDisplay = newObject.AddComponent<BoardDisplay>();
		mainBoardDisplay.Bind(mainBoard);
        mainBoardDisplay.PreInit(boardDim, boardDim, this);
		BoardDisplay tempDisplay = mainBoardDisplay as BoardDisplay;
		tempDisplay.spriteManager = mainSpriteManager;
		tempDisplay.m_XMLGems = mainXMLGems;
		tempDisplay.positionX = Screen.width * 0.5f - Screen.height * 0.4f;
		tempDisplay.positionY = Screen.height * 0.1f;
		tempDisplay.width = Screen.height * 0.8f;
		tempDisplay.height = Screen.height * 0.8f;

		newObject = new GameObject("Player");
		mainPlayer = newObject.AddComponent<Player>();
		print("System player ID: " + mainPlayer.AddPlayerInfo(new Player.PlayerInfo("System", Color.white)));
        Dictionary<int, Player.PlayerInfo> players = new Dictionary<int, Player.PlayerInfo>();
        Player.PlayerInfo main_player = new Player.PlayerInfo("Host", Color.red);
		int mainPlayerID = mainPlayer.AddPlayerInfo(main_player);
        players.Add(mainPlayerID, main_player);

        if (Data.multiplayer)
        {
            if (mainClient == null)
            {              
                print("Creating remote player..."); 
                newObject = new GameObject("Remote Player");
                mainRemotePlayer = newObject.AddComponent<RemotePlayer>();
                mainRemotePlayer.Bind(mainBoard, mainServer);
				foreach (var player in Data.players)
				{
                    int currID = mainPlayer.AddPlayerInfo(player.Value);
                    players.Add(currID, player.Value);
					mainRemotePlayer.RegisterPlayer(player.Key, currID);
				}
                mainServer.SendObjectToAll(players, Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
            }
            else if (mainClient != null)
            {
                mainClient.AddDataHandler(typeof(Dictionary<int, Player.PlayerInfo>), (message, data, instance) =>
                    {
                        mainPlayer.SetPlayers(data as Dictionary<int, Player.PlayerInfo>);
                    });
            }
        }

        mainBoard.Init(this, boardDim, boardDim);
		mainPlayer.Init(this, mainPlayerID);
        if (Data.multiplayer && mainClient != null)
        {
			mainClient.SendObject("Loaded", Lidgren.Network.NetDeliveryMethod.ReliableOrdered);
        }
	}
}